package base.utils;

import java.util.Locale;

import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;
import base.application.BaseApplication;

/**
 * This is a class help to draw our view.
 * 
 * @author fish
 * 
 */
public class DisplayHelper {

	private static final String TAG = "DisplayHelper";

	public static final int STANDARD_REFER_DISPLAY_WIDTH = 240;
	public static final int STANDARD_REFER_DISPLAY_HEIGHT = 320;
	public static final int STANDARD_REFER_DISPLAY_LENGTH = STANDARD_REFER_DISPLAY_HEIGHT;
	public static final DisplayMetrics DISPLAY_METRICS = BaseApplication.getInstance().getResources().getDisplayMetrics();
	public static final float DISPLAY_WIDTH = DISPLAY_METRICS.widthPixels;
	public static final float DISPLAY_HEIGHT = DISPLAY_METRICS.heightPixels;
	public static final float DISPLAY_SCALE = DISPLAY_METRICS.density;

	public static final float DISPLAY_SCALE_SELF = DISPLAY_WIDTH / STANDARD_REFER_DISPLAY_LENGTH;

	// static {
	// if(Logger.isLoggingEnabled())Logger.info(TAG, "The display details is :"+"\n" +
	// "DISPLAY_METRICS = "+DISPLAY_METRICS +"\n" +
	// "DISPLAY_SCALE_SELF = "+DISPLAY_SCALE_SELF +"\n" +
	// "Tips: SCALE is "+((DISPLAY_SCALE==DISPLAY_SCALE_SELF)?"same as DISPLAY_SCALE_SELF":"not the same as DISPLAY_SCALE_SELF ,ATTENYION !!"));
	// }

	public static int convertDipToPiex(int dipValue) {
		return convertDipToPiex(dipValue, false);
	}

	public static int convertPiexToDip(int piexValue) {
		return convertPiexToDip(piexValue, false);
	}

	public static int convertDipToPiex(int dipValue, boolean isUseSelfScale) {
		// if(Logger.isLoggingEnabled())Logger.info(TAG, "convertDipToPiex "+(isUseSelfScale?"useSelfScale":"userSystemScale"));
		float toReturn = (float) dipValue * getScale(isUseSelfScale, false);
		// if(Logger.isLoggingEnabled())Logger.info(TAG, "\t Convert "+dipValue+" --> "+toReturn);
		return (int) toReturn;
	}

	public static int convertPiexToDip(int piexValue, boolean isUseSelfScale) {
		// if(Logger.isLoggingEnabled())Logger.info(TAG, "convertPiexToDip");
		float toReturn;
		if (DISPLAY_SCALE != 0) {
			toReturn = (float) piexValue / getScale(isUseSelfScale, false);
		} else {
			toReturn = 0;
		}
		// if(Logger.isLoggingEnabled())Logger.info(TAG, "\t Convert "+piexValue+" --> "+(int)toReturn);
		return (int) toReturn;
	}

	private static float getScale(boolean isUserSelfScale, boolean isAuto) {
		float valueToReturn = DISPLAY_SCALE;

		if (isAuto) {
			if (DISPLAY_WIDTH > 640) {
				valueToReturn = DISPLAY_SCALE_SELF;
			}
		} else {
			if (isUserSelfScale) {
				valueToReturn = DISPLAY_SCALE_SELF;
			}
		}
		return valueToReturn;
	}

	public static int[] getLocationByPictureLocation(boolean isFormatToDip, int pictureWidth, int pictureHeight, int locationX, int locationY) {
		// if(Logger.isLoggingEnabled())Logger.info(TAG, "getLocationByPictureLocation");
		if (pictureWidth <= 0 || pictureHeight <= 0 || locationX <= 0 || locationY <= 0) {
			return null;
		}
		float xLoc = 0;
		float yLoc = 0;
		xLoc = (float) locationX / (float) pictureWidth;
		xLoc = xLoc * DISPLAY_WIDTH;
		yLoc = (float) locationY / (float) pictureHeight;
		yLoc = yLoc * DISPLAY_HEIGHT;
		if (isFormatToDip) {
			xLoc = convertPiexToDip((int) xLoc);
			yLoc = convertPiexToDip((int) yLoc);
		}
		// if(Logger.isLoggingEnabled())Logger.info(TAG, "\t Convet "+locationX+","+locationY+" --> "+(int)xLoc+","+(int)yLoc);
		return (new int[] { (int) xLoc, (int) yLoc });
	}

	public static int getDeltaByPictureDelta(boolean isFormatToDip, boolean isWidth, int pictureTotal, int pictureDelta) {
		// if(Logger.isLoggingEnabled())Logger.info(TAG, "getDeltaByPictureDelta");
		if (pictureTotal <= 0 || pictureDelta <= 0) {
			return 0;
		}
		float delta = 0;
		float displayTotal;
		if (isWidth) {
			displayTotal = DISPLAY_WIDTH;
		} else {
			displayTotal = DISPLAY_HEIGHT;
		}
		delta = (float) pictureDelta / (float) pictureTotal;
		delta = delta * displayTotal;
		// if(Logger.isLoggingEnabled())Logger.info(TAG, "\t Convert "+pictureDelta+" --> "+(int)delta);
		return (int) delta;
	}

	public static boolean isLanguageChinese() {
		// if(Logger.isLoggingEnabled())Logger.info(TAG, "isLanguageChinese");
		Locale local = Locale.getDefault();
		String language = local.getLanguage().toLowerCase();
		// if(Logger.isLoggingEnabled())Logger.info(TAG, "\t language is "+language);
		if (language.equals("zh")) {
			return true;
		}
		return false;
	}

	public static int getNotificationBarHeight(View viewInwindow) {
		Rect rect = new Rect();
		viewInwindow.getWindowVisibleDisplayFrame(rect);
		return rect.top;
	}

	public static void getViewRectInWindows(View view, Rect rect) {
		int[] location = new int[2];
		view.getLocationInWindow(location);
		rect.left = (int) location[0] + 2;
		rect.top = (int) location[1];
		rect.right = (int) (location[0] + view.getWidth());
		rect.bottom = (int) (location[1] + view.getHeight());
	}

	/**
	 * 根据相对比例还获取屏幕坐标�?
	 * 
	 * @param location
	 * @param rect
	 * @param isContainedNotificatonBar
	 * @param viewInWindow
	 */
	public static void getDisplayLocationWithPercentageLocation(float[] location, Rect rect, boolean isContainedNotificatonBar, View viewInWindow) {
		if (location == null || location.length != 4 || location[2] <= location[0] || location[3] <= location[1]) {
			return;
		}
		float displayHeight = DISPLAY_HEIGHT;
		if (!isContainedNotificatonBar) {
			if (viewInWindow == null) {
				return;
			}
			displayHeight = DISPLAY_HEIGHT - getNotificationBarHeight(viewInWindow);
		}
		rect.left = (int) (location[0] * DISPLAY_WIDTH);
		rect.top = (int) (location[1] * displayHeight);
		rect.right = (int) (location[2] * DISPLAY_WIDTH);
		rect.bottom = (int) (location[3] * displayHeight);
	}
}
